package cn.jekun.web.shop.action;

import java.sql.Types;
import java.util.HashMap;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.web.shop.bean.Shop;
import cn.jekun.web.shop.bean.Terminal;
import cn.jekun.web.shop.bean.UserShop;
import cn.jekun.web.shop.service.UserShopService;

/**
 * 操作员查询
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class UserShopAction extends ShopBaseAction {
	private static final String AUTH_SHOP_TERNIMAL_USER = "SHOP_TERNIMAL_USER";// 设备监控
	@Resource(name = UserShopService.BEAN_NAME)
	UserShopService userShopService;
	private String staffId;
	private String realName;

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	/**
	 * 查询商户下面所有的店员
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryAllByShopId() {
//		checkRight(AUTH_SHOP_TERNIMAL_USER);
//		formMap.put(Shop.COL_SHOP_ID, this.getSessionUser().getShopId());
//		formMap.put(UserShop.STAFF_ID, staffId);
//		formMap.put(UserShop.REAL_NAME, realName);
//		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap,
//				"a." + Shop.COL_SHOP_ID, Types.VARCHAR, SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"a."+UserShop.STAFF_ID,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(formMap,"b."+UserShop.REAL_NAME,Types.VARCHAR,SqlUtis.LIKE));
//		this.resultList = this.userShopService.list(formMap, page);
//		return "/shop/userShopList.jsp";
		
		JSONObject jsonObject=new JSONObject();

		try {
			String shopId=this.getSessionUser().getShopId();
			
			this.resultList = this.userShopService.getQueryUserList(shopId,staffId,realName, page);

			JSONArray jsonArray = new JSONArray();

			HashMap<String, Object> hashMap;

			for(int i=0;i<this.resultList.size();i++){

				hashMap=new HashMap<String, Object>();
				
				UserShop tempUserShop=(UserShop) resultList.get(i);

				hashMap.put(UserShop.STAFF_ID, tempUserShop.getStaffId()==null?"":tempUserShop.getStaffId());

				hashMap.put(UserShop.USER_NAME, tempUserShop.getUserName()==null?"":tempUserShop.getUserName());

				hashMap.put(UserShop.REAL_NAME, tempUserShop.getRealName()==null?"":tempUserShop.getRealName());

				hashMap.put(UserShop.POSITION_ID, tempUserShop.getPositionId()==null?"":tempUserShop.getPositionId());

				jsonArray.add(hashMap);

			}

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());

			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());

			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());

			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());

			jsonObject.put("operatorListData", jsonArray);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}

		return this.writeJson(jsonObject);
	}
	
	
	/**
	 * 修改密码
	 * @return
	 */
	public String editUserPwd(){
		
		
		JSONObject jsonObject=new JSONObject();
		
		try{
			
			String userId = this.request.getParameter("userId");
			String password = this.request.getParameter("password");;
			this.userShopService.updatePassword(userId,password);
	
			this.logService.addAdminAuditLog("", "修改用户密码成功，用户代码:"+userId);//记录操作日志
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
			
			
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

}
